$(document).ready(function(){
    $('.inputtext').focus(function(){
        $(this).css('color', '#333');
        if ($(this).val() == $(this).context.defaultValue){
            $(this).val("");
        }               
    }).blur(function(){
        if ($(this).val() == "" || $(this).val() == "Nhập tên một người bạn"){
            $(this).val($(this).context.defaultValue);
            $(this).css('color', '#838383');
        }
    });
    $('.sys_action').click(function(){     
        $('.sys_action').not($(this)).removeClass("sys_action_active");
        $('.box_action').not($(this).parent().find(".box_action")).hide();
        $(this).toggleClass("sys_action_active");
        $(this).parent().find(".box_action").toggle();
    });
    $('.box_action').hover(function(){
      
        },function(){
            $('.box_action').hide();
            $('.sys_action').removeClass("sys_action_active");
        });
    
    $('.content').click(function(){    
        $("#fri_loading").show();
        var me=$(this);
        var data = "";
        var group_id = $(this).parent().attr("id");  
        data = data+'&group_id='+group_id;  
        if($("#txtkey_friend").val()!="" && $("#txtkey_friend").val()!="Nhập tên một người bạn")
            data=data+"&keyword=" +$("#txtkey_friend").val();   
        $.ajax({
            type: "POST",
            url: "/process.php",
            data: "action=search_friend"+data,
            dataType: 'json',
            success: function(msg){               
                if(msg.status=='ok')
                {
                    $(".friend_list").html(msg.html);
                    $("#friend_drag").drag_drop_multi_select({
                        element_to_drag_drop_select:'.drag',
                        elements_to_drop:'.friend_group_box'                   
                    });
                }
                else
                {
                    $(".friend_list").html("");
                } 
                $("#group_selected").val(group_id);
                $("#num_rows").html($(".friendgroup_list #"+group_id +" .number").html());
                $(".friendgroup_list li").each(function(){
                    $(this).removeClass("active");
                });
                me.parent().parent().parent().addClass("active");
                $('.sys_action').click(function(){     
                    $('.sys_action').not($(this)).removeClass("sys_action_active");
                    $('.box_action').not($(this).parent().find(".box_action")).hide();
                    $(this).toggleClass("sys_action_active");
                    $(this).parent().find(".box_action").toggle();
                });
                $('.box_action').hover(function(){
      
                    },function(){
                        $('.box_action').hide();
                        $('.sys_action').removeClass("sys_action_active");
                    });
                $(".box_action ul li input").filter(":checkbox,:radio").checkbox();
                $(".box_action ul li").click(function(){
                    if($(this).find("input").attr("aria-checked")=="true")
                    {
                        $(this).find("input").attr("aria-checked","false");
                        $(this).find(".ui-icon").removeClass("ui-icon-check");
                        $(this).find(".ui-icon").addClass("ui-icon-empty");
                    }else
                    {
                        $(this).find("input").attr("aria-checked","true"); 
                        $(this).find(".ui-icon").addClass("ui-icon-check");
                        $(this).find(".ui-icon").removeClass("ui-icon-empty");
                    }
                });
            }
           
        });
        
        $("#fri_loading").hide();
    });
    // Set the default function
    $.fn.drag_drop_multi_select.defaults.after_drop_action = function($item,$old,$new,e,ui){
        // Possible param $item_instance, $old_container, $new_container, event, helper
        var $target = $(e.target);
        var data = "";
        var target_id = $target.attr("id");
        var friend_id=$item.attr("id");       
        var li_remove=$item.parent();
        var groups=$item.attr("rel").substr(1,$item.attr("rel").length-2);
        var arrGroups=split(",", groups);
        if(target_id=="recyclebin_g")
        {
            data = data+'&group_id='+$("#group_selected").val();
            data = data+'&friend_id='+friend_id;
            $.ajax({
                type: "POST",
                url: "/process.php",
                data: "action=removeFriendFromGroup"+data,
                success: function(msg){  
                    if(msg=='1')
                    {
                        li_remove.remove();
                        var $source=$(".friendgroup_list #"+ $("#group_selected").val());
                        $source.find(".number").html(parseInt($source.find(".number").html())-1);
                        $("#num_rows").html(parseInt($("#num_rows").html())-1);
                    }
                    else 
                    {           
                        Lightbox.showmessage("Thông báo","Có lỗi trong quá trình sử lý. Vui lòng thử lại sau.");
                            
                    }
                }
            });         
        }
        else if(target_id=="recyclebin")
        {
            data = data+'&friend_id='+friend_id;
            data = data+'&groups='+groups;
            $.ajax({
                type: "POST",
                url: "/process.php",
                data: "action=removeFriend"+data,
                success: function(msg){  
                    if(msg=='ok')
                    {
                        li_remove.remove();
                        for(var i=0;i<arrGroups.length;i++)
                        {
                            if(arrGroups[i].toString()!="")
                            {
                                var $source=$(".friendgroup_list #"+ arrGroups[i].toString());
                                $source.find(".number").html(parseInt($source.find(".number").html())-1);
                                $("#num_rows").html(parseInt($("#num_rows").html())-1);
                            }
                        }
                    }
                    else 
                    {           
                        Lightbox.showmessage("Thông báo","Có lỗi trong quá trình sử lý. Vui lòng thử lại sau.");
                            
                    }
                }
            });  
      
        }
        else
        {
          
            data = data+'&group_id='+target_id;
            data = data+'&friend_id='+friend_id;
        
       
            //alert(friend_id);
            $.ajax({
                type: "POST",
                url: "/process.php",
                data: "action=addFriend"+data,
                success: function(msg){  
                    if(msg=='1')
                    {
                        var $src_item=$item.find("img").attr("src")?$item.find("img").attr("src"):$item.attr("src");
                        var $add_item="<img id='"+$item.attr("id")+"' class='friend_inner drag' src='"+$src_item+"' title='"+$item.attr("title")+"'>";
                        //$target.append($add_item);    
                        $target.find(".number").html(parseInt($target.find(".number").html())+1);
                        $target.parent().drag_drop_multi_select({
                            element_to_drag_drop_select:'.drag',
                            elements_to_drop:'.friend_group_box'                   
                        });
                  
                        var diff=1;
                        var color_class = (diff >= 0 ? 'circle__popup_green' : 'circle__popup_red');
                        var $alert="<div class='circle__popup "+color_class+"' style='left:"+$target.left+";top:"+$target.top+"'>+"+$("#number_selected").val()+"</div>";
                        $target.append($alert); 
                        setTimeout(function() {
                            $target.find(".circle__popup").addClass('circle__popup_phase1');
                            setTimeout(function() {
                                $target.find(".circle__popup").addClass("circle__popup_phase2");   
                                setTimeout(function() {
                                    $(".circle__popup").remove();   
                                }, 1000);
                            }, 1000);
                        }, 1);
                    }
                    else if(msg=='0')
                    {                        
                    // Lightbox.showmessage("Thông báo","Thành viên này đã tồn tại trong nhóm.");
                    }
                    else 
                    {           
                        Lightbox.showmessage("Thông báo","Có lỗi trong quá trình sử lý. Vui lòng thử lại sau.");
                            
                    }
                }
            });
        }     
              
    };
            

   
    // Initiate the drag & drop
          

    $('#MySplitter').drag_drop_multi_select({
        element_to_drag_drop_select:'.drag',
        elements_to_drop:'.friend_group_box'
    });

 
    $('.friend_inner').hover(function(){
        $(this).addClass("friend_inner_hover", 200);   
    },function(){
        $(this).removeClass("friend_inner_hover",200);   
    });
    $('.recyclebin').hover(function(){
        $(this).addClass("drop_hover", 0);   
    },function(){
        $(this).removeClass("drop_hover",0);   
    });
    $('.friend_group_box').hover(function(){
        $(this).addClass("drop_hover", 200);   
    },function(){
        $(this).removeClass("drop_hover",100);   
    });
    $('.friendgroup_list .add').hover(function(){
        $(this).addClass("drop_hover", 300);
    },function(){
        $(this).removeClass("drop_hover",200); 
    });
    //drawCircle('.friend_inner');
    
    $(".reset").click(function(){
        $("#txtkey_friend").val("");
        var data = "";
        var keyword = $(this).val();
        if(keyword!="" && keyword!="Nhập tên một người bạn")
            $(".reset").show();
        else
            $(".reset").hide();
        var group_id = "";
        data = data+'&keyword='+keyword;
        data = data+'&group_id='+group_id;
       
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/process.php",
            data: "action=search_friend"+data,
            success: function(msg){   
                if(msg.status=='ok')
                {
                    
                    $(".friend_list").html(msg.html);
                }
                else
                {
                    $(".friend_list").html("");
                }
            }
        });
    })
    $("#txtkey_friend").keyup(function(){
        
        var data = "";
        var keyword = $(this).val();
        if(keyword!="" && keyword!="Nhập tên một người bạn")
            $(".reset").show();
        else
            $(".reset").hide();
        var group_id = $("#group_selected").val();
        data = data+'&keyword='+keyword;
        data = data+'&group_id='+group_id;
       
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/process.php",
            data: "action=search_friend"+data,
            success: function(msg){   
                if(msg.status=='ok')
                {
                    $(".friend_list").html(msg.html);
                    $("#friend_drag").drag_drop_multi_select({
                        element_to_drag_drop_select:'.drag',
                        elements_to_drop:'.friend_group_box'                   
                    });
                    $("#num_rows").html(msg.num);
                }
                else
                {
                    $(".friend_list").html("");
                }
                $('.sys_action').click(function(){     
                    $('.sys_action').not($(this)).removeClass("sys_action_active");
                    $('.box_action').not($(this).parent().find(".box_action")).hide();
                    $(this).toggleClass("sys_action_active");
                    $(this).parent().find(".box_action").toggle();
                });
                $('.box_action').hover(function(){
      
                    },function(){
                        $('.box_action').hide();
                        $('.sys_action').removeClass("sys_action_active");
                    });
                $(".box_action ul li input").filter(":checkbox,:radio").checkbox();
                $(".box_action ul li").click(function(){    
                    if($(this).find(".ui-checkbox").length)
                    {
                        if($(this).find("input").attr("aria-checked")=="true")
                        {
                            var data = "";
                            var group_id=$(this).find("input").filter(":checkbox,:radio").attr("id");
                            var friend_id=$(this).parent().attr("rel");
                            data = data+'&friend_id='+friend_id;
                            data = data+'&group_id='+group_id;
                            $.ajax({
                                type: "POST",
                                dataType: 'json',
                                url: "/process.php",
                                data: "action=removeFriendFromGroup"+data,
                                success: function(msg){                     
                                    if(msg!='-1')
                                    {
                      
                      
                                    }
                                    else
                                    {
                        
                                }
                                }
                            });
                            $(this).find("input").attr("aria-checked","false");
                            $(this).find(".ui-icon").removeClass("ui-icon-check");
                            $(this).find(".ui-icon").addClass("ui-icon-empty");
                            var $source=$(".friendgroup_list #"+ group_id);
                            $source.find(".number").html(parseInt($source.find(".number").html())-1);
                        }else
                        {
                            var data = "";
                            var group_id=$(this).find("input").filter(":checkbox,:radio").attr("id");
                            var friend_id=$(this).parent().attr("rel");
                            data = data+'&friend_id='+friend_id;
                            data = data+'&group_id='+group_id;
                            $.ajax({
                                type: "POST",
                                dataType: 'json',
                                url: "/process.php",
                                data: "action=addFriend"+data,
                                success: function(msg){                     
                                    if(msg!='-1')
                                    {
                       
                                    }
                                    else
                                    {
                        
                                }
                                }
                            });
                            $(this).find("input").attr("aria-checked","true"); 
                            $(this).find(".ui-icon").addClass("ui-icon-check");
                            $(this).find(".ui-icon").removeClass("ui-icon-empty");
                            var $source=$(".friendgroup_list #"+group_id);
                            $source.find(".number").html(parseInt($source.find(".number").html())+1);
                        }
                    }
                }); 
            }
        });
    });

//    var $sidebar   = $(".friendgroup_container");  
//    var $sidebar_act   = $("#divrecyclebin");  
//    if($('#friend_drag').length > 0){
//        var descripY = parseInt($('#friend_drag').offset().top);
//        var descripX = parseInt($sidebar.offset().left);   
//        var descripX_act = parseInt($sidebar_act.offset().left);    
//        $(window).scroll(function () {         
//            var scrollY = $(window).scrollTop();
//            var fixedShare = $sidebar.css('position') == 'fixed';
//            var fixedShare_act = $sidebar_act.css('position') == 'fixed';
//            if($sidebar.length > 0){
//                if ( scrollY > descripY && !fixedShare ) {
//                    $sidebar.stop().css({
//                        position: 'fixed',
//                        top: 5,
//                        left:descripX
//                    });
//                } else if ( scrollY < descripY && fixedShare ) {
//                    $sidebar.css({
//                        position: 'relative',  
//                        top: 0,
//                        left:0
//                    });
//                }
//            }
//            if($sidebar_act.length > 0){
//                if ( scrollY > descripY && !fixedShare_act ) {
//                    $sidebar_act.stop().css({
//                        position: 'fixed',
//                        top: 5,
//                        left:descripX_act
//                    });
//                } else if ( scrollY < descripY && fixedShare_act ) {
//                    $sidebar_act.css({
//                        position: 'absolute',  
//                        top: 100,
//                        left:38
//                    });
//                }
//            }
//        });
//    }
        
    $(".box_action ul li input").filter(":checkbox,:radio").checkbox();
    $(".box_action ul li").click(function(){    
        if($(this).find(".ui-checkbox").length)
        {
            if($(this).find("input").attr("aria-checked")=="true")
            {
                var data = "";
                var group_id=$(this).find("input").filter(":checkbox,:radio").attr("id");
                var friend_id=$(this).parent().attr("rel");
                data = data+'&friend_id='+friend_id;
                data = data+'&group_id='+group_id;
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    url: "/process.php",
                    data: "action=removeFriendFromGroup"+data,
                    success: function(msg){                     
                        if(msg!='-1')
                        {
                      
                      
                        }
                        else
                        {
                        
                    }
                    }
                });
                $(this).find("input").attr("aria-checked","false");
                $(this).find(".ui-icon").removeClass("ui-icon-check");
                $(this).find(".ui-icon").addClass("ui-icon-empty");
                var $source=$(".friendgroup_list #"+ group_id);
                $source.find(".number").html(parseInt($source.find(".number").html())-1);
            }else
            {
                var data = "";
                var group_id=$(this).find("input").filter(":checkbox,:radio").attr("id");
                var friend_id=$(this).parent().attr("rel");
                data = data+'&friend_id='+friend_id;
                data = data+'&group_id='+group_id;
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    url: "/process.php",
                    data: "action=addFriend"+data,
                    success: function(msg){                     
                        if(msg!='-1')
                        {
                       
                        }
                        else
                        {
                        
                    }
                    }
                });
                $(this).find("input").attr("aria-checked","true"); 
                $(this).find(".ui-icon").addClass("ui-icon-check");
                $(this).find(".ui-icon").removeClass("ui-icon-empty");
                var $source=$(".friendgroup_list #"+group_id);
                $source.find(".number").html(parseInt($source.find(".number").html())+1);
            }
        }
    });
    $(".rm").click(function(){
        var data = "";
        var li_remove=$(this).parent().parent().parent();
        var friend_id=$(this).parent().attr("rel");
        var groups=$(this).parent().attr("lang").substr(1,$(this).parent().attr("lang").length-2);
        var arrGroups=split(",", groups);
        data = data+'&friend_id='+friend_id;
        data = data+'&groups='+groups;
        $.ajax({
            type: "POST",
            url: "/process.php",
            data: "action=removeFriend"+data,
            success: function(msg){  
                if(msg=='ok')
                {
                    li_remove.remove();
                    for(var i=0;i<arrGroups.length;i++)
                    {
                        if(arrGroups[i].toString()!="")
                        {
                            var $source=$(".friendgroup_list #"+ arrGroups[i].toString());
                            $source.find(".number").html(parseInt($source.find(".number").html())-1);
                            $("#num_rows").html(parseInt($("#num_rows").html())-1);
                        }
                    }
                }
                else 
                {           
                    Lightbox.showmessage("Thông báo","Có lỗi trong quá trình sử lý. Vui lòng thử lại sau.");
                            
                }
            }
        });  
    })
}); 
function showCreateGroup(){
    Lightbox.show("createGroup",400,215,"Tạo mới nhóm bạn","html");

}
function toggleToGroup(friend_id,group_id){
    var data = "";
    data = data+'&friend_id='+friend_id;
    data = data+'&group_id='+group_id;
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "/process.php",
        data: "action=search_friend"+data,
        success: function(msg){   
            if(msg.status=='ok')
            {
                    
                $(".friend_list").html(msg.html);
            }
            else
            {
                $(".friend_list").html("");
            }
        }
    });

}
function CreateGroup() {
           
    $(".loading").show();
    var data = "";
    var name = $('#lightbox #i_name').val();
    var desc = $('#lightbox #i_desc').val();
    
    data = data+'&group_name='+name;
    data = data+'&desc='+desc;
            
    $.ajax({
        type: "POST",
        url: "/process.php",
        data: "action=create_group"+data,
        success: function(msg){  
            if(msg=='ok')
                window.location.reload();
            else
            {
                $(".loading").hide();
                Lightbox.hide();
                Lightbox.showmessage("Thông báo","Có lỗi trong quá trình sử lý. Vui lòng thử lại sau.");
            }
        }
    });
}
